<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quản lý quyền</title>

<link href="../Twitter_Bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="../CSS/css_admin.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="../CSS/validationEngine.jquery.css"/>

<script type="text/javascript" src="../Twitter_Bootstrap/js/jquery.js"></script>
<script type="text/javascript" src="../Twitter_Bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../Scripts/jquery.validationEngine.js"></script>
<script type="text/javascript" src="../Scripts/jquery.validationEngine-vi.js"></script>
<script type="text/javascript" src="../Scripts/RoleManagementJS.js"></script>


<style type="text/css">
<!--
#tbResult td,#tbResult th
{
	text-align:center;
}
-->
</style>
</head>

<body>
	<div class="container" id="Container">
<!--Menu top -->
  <div id="menu">
  	<?php
		include("MainMenu.php");
		include("../Connections/connect_admin.php");
	$page=1;
		$recordPerPage=10;
		if(isset($_GET["page"]))
			{
				$page=intval($_GET["page"]);
			}
		$index=($page-1)*$recordPerPage;
		$keyword='';
		 if(isset($_GET["keyword"]))
	  {
		$keyword=trim($_GET["keyword"]);
 		$query="SELECT * FROM role r WHERE IsDeleted=0 AND (r.RoleID LIKE '%$keyword%' OR r.RoleName LIKE '%$keyword%' ) ORDER BY r.RoleID ";
		
	  }
	  else
	  {
		$query="SELECT * FROM role r WHERE IsDeleted=0 ORDER BY r.RoleID ";
	  }
	  $sql=$query." Limit $index,$recordPerPage";
	  $result=mysql_query($sql);
	?>
    </div>
  </div>
  <div id="divContain">
  <!--Content -->
   <div class="divTitle"><span class="titlePage" >Quản lý quyền</span></div>
  	<div style="padding-top:30px" >
     <form action="" method="get" id="formSearch" class="form-horizontal">
 		<div class="input-append span7">
        <input type="text" id="txtKeyword" placeholder="Từ khóa..." name="keyword" <?php if($keyword!='') echo "value='$keyword'"; ?>><button class="btn" type="submit" id="btnSearch"><i class=" icon-search"></i> Tìm kiếm</button>
       
        </div>
         <a type="button" class="btn btn-primary" id="btnAddRole" href="#updateRole" data-toggle="modal"><i class="icon-white icon-plus-sign"></i> Tạo quyền mới</a>
    </form>
     <?php
	if(isset($_GET["e"]))
	{
		$error=intval($_GET["e"]);
		switch($error)
		{
			case 0:
	?>
    <div class="alert alert-success">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Thêm mới quyền THÀNH CÔNG</strong>
    </div>
    <?php
			break;
			case 1:
	?>
     <div class="alert alert-error">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Thêm mới quyền THẤT BẠI</strong>
    </div>
     <?php
	 		break;
			case 2:
	?>
     <div class="alert alert-success">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Cập nhật quyền THÀNH CÔNG</strong>
    </div>
     <?php
	 		break;
			case 3:
	?>
     <div class="alert alert-error">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Cập nhật quyền THẤT BẠI</strong>
    </div>
    <?php
			break;
		}	
		
	}
	?>
    
    <table class="table table-bordered table-striped  " id="tbResult" width="100%" border="0" cellspacing="0" cellpadding="0" >
  <tr>
  	<th scope="col">Mã quyền</th>
    <th scope="col">Tên quyền</th>

    <th scope="col">Mô tả</th>
    <th scope="col">&nbsp;</th>
    <th scope="col">&nbsp;</th>
  </tr>
  <?php
  while($row=mysql_fetch_assoc($result))
  {
  ?>
  <tr>
    <td><?php echo $row["RoleID"]; ?></td>
    <td><?php echo $row["RoleName"]; ?></td>
  
    <td><?php echo $row["Description"]; ?></td>
    <td><a href="#" title="Xóa" class="delete"><img src="../Images/Icons/delete.png" alt="delete"/></a></td>
    <td><a href="#updateRole" data-toggle="modal" title="Sửa" class="update"><img src="../Images/Icons/page_edit.png" alt="update" /></a></td>
  </tr>
  <?php }?>
</table>
	    <div class="pagination pull-right">
   <?php require("process/funcPaging.php");
			
			if(isset($_GET["keyword"]))
			{
				$keyword=$_GET["keyword"];
				$link="Role.php?keyword=".$keyword."&";
			}
			else
			{	
				$link="Role.php?";
			}
			$resAll=mysql_query($query);
			$total=mysql_num_rows($resAll);
			
			Paging($total,$page,5,$recordPerPage,$link);
 		?>  
    </div>
	</div>
  <!--footer -->
  </div>
  <div id="footer">
     <?php
     	include("Footer.php");
	 ?>
  </div>
</div>

<!---------------------------Popup Thêm/Cập nhật người dùng----------------------------->
    <div class="modal hide fade"  id="updateRole">
    <div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h3></h3>
    </div>
    <form action="process/Role_process.php" method="post" id="formRole" style="margin-bottom:0px">
    <div class="modal-body">
    <p>
    	<input type="hidden" id="task" name="task" />
		 <input type="hidden" id="allow" value="true" />
        	<table width="100%" border="0" cellspacing="0" cellpadding="0">
            	<tr>
                    <td>Mã quyền</td>
                    <td><input name="roleid" type="text" id="txtRoleID" class="validate[required,funcCall[checkSpLetter]]" /></td>
                  </tr>
                  <tr>
                    <td>Tên quyền</td>
                    <td><input name="rolename" type="text" id="txtRoleName" class="validate[required]" /></td>
                  </tr>
                  <!--<tr>
                    <td>Action:</td>
                    <td><input name="" type="text" id="txtAction" /></td>
                  </tr> -->
                  <tr>
                    <td>Mô tả:</td>
                    <td><textarea id="txtDescription" rows="3" name="desc"></textarea></td>
                  </tr>
                  
              </table>

       
    </p>
    </div>
    <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Đóng lại</a>
    <button type="submit" class="btn btn-primary" id="btnSave">Cập nhật</button>
    </div>
     </form>
     <script>
		//-----------------------Validation----------------------------------
			$(document).ready(function(){
							   $("#formRole").validationEngine();
		//-----------------------Check if RoleName existed----------------------------------
	$('#txtRoleID').keyup(function(){
										  
								var t=this;
								if(this.value!=this.LastValue)
								{
									if(this.timer)
									{
										clearTimeout(this.timer);
									}
									$('span#check').remove();
									//alert($(this).val());
									if($(this).val()!='')
									{
									$(this).after('<span id="check" style="font-size:10px"><img src="../Images/Icons/ajax-loader.gif" alt=""/>Checking...</span>');
									this.timer = setTimeout(function () {
										$.ajax({
											url: 'process/Role_process.php',
											data: { roleid: t.value,task:'checkExist' },
											dataType: 'json',
											type: 'post',
											success: function (j) {
												// checking debug
												if(j.ok) {
													$('span#check').remove();
													$('#txtRoleID').after(' <span id="check" style="color:red; font-size:10px"> <img src="../Images/Icons/delete.png" alt="" /> '+j.msg+'</span>');
													$('#allow').val('false');
												} else {
													$('span#check').remove();
													$('#txtRoleID').after(' <span id="check" style="color:green; font-size:10px"> <img src="../Images/Icons/accept.png" alt="" /> '+j.msg+'</span>');
													$('#allow').val('true');
												}
												/* put the 'msg' field from the $resp array from 
												check_username (php code) in to the validation message */
												//validateUsername.html(j.msg);
											}
										});
									}, 200);
									this.lastValue = this.value;
									}
								}
										 });
	
	$('#formRole').submit(function(){	
									   if($('#allow').val()=='true')
										   return true;
										return false;
										   });
	
							   });
		</script>
    </div>

</body>
</html>
